using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Runtime.CompilerServices;

namespace FeedFly
{
    /// <summary>
    /// Class for holding feed item information
    /// </summary>
    public class FeedItem
    {
        private int id;
        private int feedId;
        private Feed feed;
        private string title;
        private string link;
        private string guid;
        private string description = string.Empty;
        private DateTime pubDate = DateTime.MinValue;
        private string author = string.Empty;
        private bool isRead;

        public string Author
        {
            get { return this.author; }
            set { this.author = value; }
        }

        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }

        public bool IsRead
        {
            get { return this.isRead; }
            set { this.isRead = value; }
        }

        public int Id
        {
            get { return this.id; }
            set { this.id = value; }
        }

        public string Guid
        {
            get { return guid; }
            set { guid = value; }
        }

        public string Link
        {
            get { return this.link; }
            set { this.link = value; }
        }

        public Feed Feed
        {
            get { return this.feed; }
            set { this.feed = value; }
        }

        public int FeedId
        {
            get { return this.feedId; }
            set { this.feedId = value; }
        }

        public DateTime PubDate
        {
            get { return this.pubDate; }
            set { this.pubDate = value; }
        }

        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }

        public FeedItem(Feed feed)
        {
            this.feed = feed;
        }

        public FeedItem()
        { }

        public static FeedItem GetItem(int itemId)
        {
            FeedItem item = null;
            SqlCeDataReader rdr = Data.ExecuteReader("FeedItem", "PK_FeedItem", itemId);

            StringBuilder sql = new StringBuilder("SELECT * FROM FeedItem WHERE Id = ", 40);

            String description = null, descriptionEx = null;
            StringBuilder descBuilder;

            if (rdr.Read())
            {
                item = new FeedItem();

                item.Id = itemId;
                item.FeedId = rdr.GetInt32(1);
                item.Title = rdr.GetString(2);
                item.Link = rdr.GetString(3);
                item.PubDate = rdr.GetDateTime(4);

                if (!rdr.IsDBNull(5))
                    item.Author = rdr.GetString(5);
                
                item.IsRead = rdr.GetBoolean(6);

                description = rdr.GetString(7);

                if(rdr.IsDBNull(8))
                    item.Description = description;
                else
                {
                    descriptionEx = rdr.GetString(8);
                    descBuilder = new StringBuilder(description, description.Length + descriptionEx.Length);                    
                    item.Description = descBuilder.Append(descriptionEx).ToString();
                }

                if (!rdr.IsDBNull(9))
                    item.Guid = rdr.GetString(9);

            }

            rdr.Close();
            rdr.Dispose();

            return item;
        }

        public static SqlCeDataReader GetItemDataReader(int feedId)
        {
            return Data.ExecuteReader("FeedItem", "FeedItem_MostRecentByFeed", feedId);
        }


        public struct MarkRequest
        {
            public int ItemId;
            public int FeedId;
            public int FolderId;
            public bool IsRead;

            public MarkRequest(int itemId, int feedId, int folderId, bool isRead)
            {
                ItemId = itemId;
                FeedId = feedId;
                FolderId = folderId;
                IsRead = isRead;
            }
        }

        /// <summary>
        /// Marks feed item as read.  This should be called asynchronously
        /// </summary>
        /// <param name="itemId"></param>
        /// <param name="feedId"></param>
        /// <param name="isRead"></param>
        public static void Mark(MarkRequest mr)
        {
            short unreadIncrement = -1;

            if (!mr.IsRead) unreadIncrement = 1;
           
            SqlCeConnection conn = Data.GetConnection();
            SqlCeTransaction trans = conn.BeginTransaction();

            SqlCeCommand cmd = new SqlCeCommand("UPDATE FeedItem SET isRead = @isRead WHERE Id = @Id", conn, trans);
            cmd.Parameters.Add(new SqlCeParameter("@isRead",mr.IsRead));
            cmd.Parameters.Add(new SqlCeParameter("@Id", mr.ItemId));

            cmd.ExecuteNonQuery();

            // increment or decrement denormalized feed count
            Feed.AdjustUnreadCount(mr.FeedId, mr.FolderId, unreadIncrement, conn, trans);

            trans.Commit();
            conn.Close();
            conn.Dispose();
        }
    }


}
